/***********************************************************
 * Product Name: ExcelQuicker
 * Copyright (c)2005, Eunge
 * Author: Eunge
 * Web Site: www.eudev.net
 * Mail/MSN: eudev.net@yeah.net
 * ********************************************************/
using System.Data;
using System;

namespace ExcelQuicker.Framework
{
	/// <summary>
	/// Batch arithmetic for outputing datatable
	/// </summary>
	[Serializable]
	public class BatchArithmetic: EQObject,  IBatchMode 
	{
		/// <summary>
		/// Implement interface
		/// </summary>
		/// <param name="objRange"></param>
		/// <param name="objEQTableBase"></param>
		public void ArithmeticOutput(Excel.Range objRange, EQTableBase objEQTableBase)
		{
			Excel.Worksheet objWorksheet = objRange.Worksheet;
			Excel.Range objColumnRange = null;
			for(int i=0; i<objEQTableBase.ColumnsDataType.Length; i++)
			{
				if(objEQTableBase.ColumnsDataType[i] == EQType.Text)
				{
					int intColumnIndex = objEQTableBase.ItemLeft + i;
					int bottom = objEQTableBase.ItemTop + objEQTableBase.Table.Rows.Count - 1;
					objColumnRange = (Excel.Range)objWorksheet.get_Range(objWorksheet.Cells[objEQTableBase.ItemTop, intColumnIndex], objWorksheet.Cells[bottom, intColumnIndex]);
					objColumnRange.NumberFormatLocal = "@";
				}
			}
			objRange.Value = ConvertDataTableToMatrixObject(objEQTableBase.Table);
		}


		/// <summary>
		/// Convert datatable to object[,]
		/// </summary>
		/// <param name="dtbl"></param>
		/// <returns>object[,]</returns>
		private static object[,] ConvertDataTableToMatrixObject(DataTable dtbl)
		{
			int rowsCount = dtbl.Rows.Count;
			int columnCount = dtbl.Columns.Count;
			object[,] objMatrix = new object[rowsCount,columnCount];
			for(int i=0; i<rowsCount; i++)
				for(int j=0; j<columnCount; j++)
					objMatrix[i,j] = dtbl.Rows[i][j];
			return objMatrix;
		}
	}
}
